﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF_calculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void add_Click(object sender1, RoutedEventArgs e1)
        {
            Operator.Content = "+";
        }
        private void sub_Click(object sender2, RoutedEventArgs e2)
        {
            Operator.Content = "-";
        }
        private void mul_Click(object sender3, RoutedEventArgs e3)
        {
            Operator.Content = "*";
        }
        private void div_Click(object sender4, RoutedEventArgs e4)
        {
            Operator.Content = "/";
   
    }

        private void equal1_Click(object sender, RoutedEventArgs e)
        {
            //输入是否为空
            if (number_1.Text == "" && number_2.Text == "")
                MessageBox.Show("请输入数据");
            else
            {
                double number1, number2;
                if(number_2.Text=="")
                {
                    number1 = Convert.ToDouble(number_1.Text);
                    number2 = 0;
                }
                else
                {
                    number1 =Convert.ToDouble(number_1.Text); 
                    number2 =Convert.ToDouble(number_2.Text);
                }
                switch (Convert.ToChar(Operator.Content))
                {

                    case '+':
                        result.Content = (number1 + number2).ToString();
                        break;
                    case '-':
                        result.Content = (number1 - number2).ToString();
                        break;
                    case '*':
                        result.Content = (number1 * number2).ToString();
                        break;
                    case '/':
                        if (number2 == 0)
                            result.Content = "除数不能为零";
                        else
                            result.Content = (number1 / number2).ToString();
                        break;
                }
            }
        }


        private void clear_Click(object sender, RoutedEventArgs e)
        {
            number_1.Text = "";
            number_2.Text = "";
            result.Content = "";
        }
    }
}
